<?php
if(!isset($_SESSION)) session_start();


$directory = "{$_SERVER["DOCUMENT_ROOT"]}/modules/social_media/news";

   // create a handler to the directory
    $dirhandler = opendir($directory);
 
    // read all the files from directory
    $nofiles=0;
    while ($file = readdir($dirhandler)) {
 
        // if $file isn't this directory or its parent 
        //add to the $files array
        if ($file != '.' && $file != '..')
        {	
			$files[$nofiles]=$file;    
			$nofiles++;            
        }   
    }
   
 if (! isset($_SESSION["last_news"]))   {
 	$file = "{$directory}/{$files[0]}";
 	$_SESSION["last_news"] = $files[0];
 } else {
 	//Get next file;
 	$key = array_search($_SESSION["last_news"], $files);
 	$key++;
 	if ($key >=  count($files)) $key=0;
 	$file = "{$directory}/{$files[$key]}";
 	$_SESSION["last_news"] = $files[$key];
 }
 $text = file_get_contents($file);
 $timestamp = date("d M Y H:i", filectime($file));
 if ($text != "") echo "{$timestamp}:{$text}";  
 else echo "There is no new news";
?>